<template>
  <el-dialog ref="dislogRef" :visible.sync="dialogVisible" lock-scroll width="35%" height="50%">
    <div slot="title" class="header_title">
      <span class="title_span">选择商品类别</span>
    </div>
    <div class="content_div">
      <div class="info_table">
        <div class="div_tree">
          <div class="tree_input">
            <p>
              <input type="text" @change="onSelectTree()" placeholder="查找" v-model="typeName" />
              <span @click="onSelectTree()">
                <i class="el-icon-search"></i>
              </span>
            </p>
          </div>
          <div class="tree_structure">
            <el-tree
              :data="treeData"
              node-key="label"
              :props="defaultProps"
              highlight-current
              default-expand-all
              ref="tree"
              :filter-node-method="filterNode"
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
            ></el-tree>
          </div>
        </div>
      </div>
      <div class="info_button">
        <el-button type="primary" @click="onSelectRow">选择</el-button>
        <el-button @click="onCancelRow">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { getAction } from '@/api/manage'
export default {
  name: 'BasicInformation',
  computed: {
    ...mapGetters(['name']),
  },
  props: {
    storehouseId: '',
  },
  created () {

  },
  mounted () {
  },
  watch: {
    dialogVisible (val) {
      if (val == true) {
        this.getData()
      }
    },
    typeName (val) {
      this.$refs.tree.filter(val);
    }
  },
  data () {
    return {
      typeName: '',
      queryData: {
        xghGoodsTypeId: '',
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      selectRow: {}, //当前选择的数据
      dialogVisible: false, // 新增弹框
    }
  },
  methods: {
    tableRowClassName ({ row }) {
      if (row.id === this.selectRow.id) {
        return 'select_row'
      }
    },
    // 获取数据
    getData () {
      this.getTreeData()
    },
    // 获取类别数据
    getTreeData() {
      getAction('/xghGoodsType/xghGoodsType/typeList', { }).then((res) => {
        if (res.code === 200) {
          let list = res.result
          let result = this.getTreeDataByList(list, '0', '')
          this.treeData = result
        }
      })
    },
    getTreeDataByList (list, parentId, parentName) {
      let data = []
      for (let index = 0; index < list.length; index++) {
        const element = list[index];
        if (element.pid == parentId) {
          data.push({
            ...element,
            id: element.id,
            label: element.goodsTypeName,
            parentId,
            parentName,
            children: this.getTreeDataByList(list, element.id, element.companyTypeName)
          })
        }
      }
      return data
    },
    // 搜索
    onQuery () {
      this.getTreeData()
    },
    // 全屏显示
    toDetail () {
      // this.$router.push({
      //   name: 'commodityInfo',
      // })
    },
    // 搜索树形结构数据
    onSelectTree () {
      this.getTreeData()
    },
    // 选择树形当前节点
    handleNodeClick (data) {
      this.queryData.xghGoodsTypeId = data.id
    },
    // 商品类别数据筛选
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 选中后传递给父组件
    onSelectRow () {
      if (!this.queryData.xghGoodsTypeId) {
        this.$elMessage.warning('请选择一条信息')
      } else {
        this.$emit('onSelectCommodityType', this.queryData.xghGoodsTypeId)
        this.dialogVisible = false
      }
    },
    // 取消
    onCancelRow () {
      this.$emit('onCancelCommodity', this.selectRow)
      this.dialogVisible = false
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 100%;
  .el-dialog__body {
    padding: 0;
  }
  .el-dialog__header {
    width: 100%;
    height: 0.937559rem;
    box-sizing: border-box;
    padding: 0 0.187512rem 0 0.281268rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dialog__headerbtn {
      position: static;
      .el-icon-close {
        color: #999;
        font-size: 30px;
      }
    }
  }
  .header_title {
    width: 100%;
    display: flex;
    align-items: center;
    .title_span {
      white-space: nowrap;
      font-size: 24px;
      color: #666;
      font-weight: 600;
    }
    .title_search {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-sizing: border-box;
      padding: 0 0.468779rem 0 1.875117rem;
      > div {
        display: flex;
        align-items: center;
        .el-input {
          width: 4.725295rem;
          height: 0.562535rem;
          margin-right: 0.112507rem;
          .el-input__inner {
            height: 0.562535rem;
            border-color: #dddddd;
            box-sizing: border-box;
            padding: 0 0.150009rem;
            background: #fff;
            font-size: 12px;
            color: #666;
            &::placeholder {
              font-size: 12px;
              color: #c1c1c1;
            }
          }
        }
        .el-button {
          width: 1.12507rem;
          height: 0.562535rem;
          text-align: center;
          box-sizing: border-box;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 12px;
          border-radius: 0.093756rem;
          border: none;
          margin-right: 0.112507rem;
          span {
            color: #fff;
            font-size: 12px;
            font-weight: normal;
          }
        }
      }
    }
    .set_up {
      font-size: 23px;
      color: rgb(153, 153, 153);
    }
  }
}
.content_div {
  width: 100%;
  height: 60vh;
  background: #fff;
  box-sizing: border-box;
  padding: 0.187512rem 0.281268rem;
  .info_table {
    width: 100%;
    height: calc(100% - 0.750047rem);
    .div_tree {
      height: calc(100% - 1.12507rem);
      padding: 0.131258rem 0;
      transition: all 0.3s;
      
      .tree_input {
        width: 100%;
        height: 0.562535rem;
        margin-top: 0.131258rem;
        box-sizing: border-box;
        padding: 0 0.168761rem;
        p {
          width: 100%;
          height: 0.562535rem;
          margin: 0;
          box-sizing: border-box;
          border: 1px solid #dddddd;
          display: flex;
          align-items: center;
          position: relative;
          input {
            width: 100%;
            box-sizing: border-box;
            padding: 0 0.600038rem 0 0.093756rem;
            height: 0.525033rem;
            border: none;
            font-size: 12px;
            &:focus {
              outline: none;
            }
          }
          span {
            position: absolute;
            right: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 0.562535rem;
            height: 0.525033rem;
            background: #fff;
            .el-icon-search {
              font-size: 16px;
              color: rgb(211, 211, 211);
            }
          }
        }
      }
      .tree_structure {
        width: 100%;
        height: calc(100% - 0.337521rem - 0.131258rem - 0.131258rem - 0.562535rem);
        box-sizing: border-box;
        padding: 0 0.168761rem;
        margin-top: 0.131258rem;
        overflow: auto;
        ::v-deep .el-tree {
          width: 100%;
          .el-tree-node__children {
            .el-tree-node__label {
              color: #666;
              font-weight: normal;
            }
          }
          .el-tree-node__label {
            font-size: 12px;
            font-weight: 600;
          }
          .is-current {
            .el-tree-node__content {
              background-color: #e6f7ff;
            }
          }
          .el-tree-node__content > .el-tree-node__expand-icon {
            padding-left: 0;
          }
        }
      }
    }
  }
  .info_button {
    width: 100%;
    height: 0.750047rem;
    display: flex;
    justify-content: flex-end;
    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.093756rem;
    }
  }
}
::v-deep .select_row {
  background-color: #e3f6ff !important;
}
</style>